package com.example.demo.comtroller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.demo.pojo.User;
import com.example.demo.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;

	// 添加用户
	@RequestMapping("/addUser")
	public String addUser(User user) {
		userService.addUser(user);
		return "redirect:/user/showUsers";
	}

	// 查询所有用户
	@RequestMapping("/showUsers")
	public String showUsers(Model model) {
		List<User> users = userService.findAllUser();
		model.addAttribute("users", users);
		return "showUsers";
	}

	// 查询单个用户，用户操作
	@RequestMapping("/findUserById")
	public String findUserById(Integer id, Model model) {
		User user = userService.findUserById(id);
		model.addAttribute("user", user);
		return "updateUser";
	}

	// 更新用户
	@RequestMapping("/editUser")
	public String editUser(User user, Model model) {
		userService.changeUser(user);
		return "redirect:/user/showUsers";
	}
	
	// 删除用户
	@RequestMapping("/deleteUserById")
	public String deleteUserById(Integer id) {
		userService.deleteUserById(id);
		return "redirect:/user/showUsers";
	}

	/**
	 * 页面跳转
	 */
	@RequestMapping("/{page}")
	public String showPage(@PathVariable String page) {
		return page;
	}
}
